<?php
// 引入公共文件
include_once('./config/config.php');
// 引入数据库配置文件
include_once('./config/check.php');

//检验用户名
$action = isset($_GET['action'])?$_GET['action']:'';

if ($action == 'add') {
  $result = [
    'code' => 0,
    'msg' => ''
  ];

  $username = isset($_POST['username'])?$_POST['username']:'';

  $sql = "select * from per_admin where username='$username'";

  $admin = find($sql);
  if ($admin) {
    $result['code'] = 0;
    $result['msg'] = '用户名已存在';
  }else {
    $result = [
      'code' => 1,
      'msg' => '可以注册'
    ];
  }
  echo json_encode($result);
  exit;
}

if ($_POST) {
  $username = isset($_POST['username'])?$_POST['username']:'';
  $password = isset($_POST['password'])?$_POST['password']:'';

  $sql = "SELECT * FROM per_admin WHERE username='$username'";
  $result = find($sql);
  if ($result) {
    ShowMsg('用户名已注册，请重新输入');
    exit;
  }

  // 生成密码盐
  $salt = build_randstr();
  // 密码加密
  $password = md5($password.$salt);
  $data = [
    'username' => $username,
    'password' => $password,
    'salt' => $salt
  ];
  // $result = add('per_admin',$data);
  // if (!$result) {
  //   ShowMsg('添加失败');
  //   exit;
  // }
  // ShowMsg('添加成功','adminList.php');
  // exit;

   //判断是否修改头像
   if ($_FILES['avatar']['size'] > 0 && $_FILES['avatar']['error'] == 0)
   {
     $res = upload('avatar');
     if ($res['code'] == 0)
     {
       ShowMsg($res['msg']);
       exit;
     }
     $data['avatar'] = $res['data'];
   }
 
   $result = add('per_admin', $data);
 
   if (!$result)
   {
     ShowMsg('添加失败');
     exit;
   }
 
   ShowMsg('添加成功', 'adminList.php');
   exit;
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
  <?php include_once('common/meta.php');?>
  </head>
  <body> 
    
    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">添加管理员</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.html">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                    
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 管理员列表</button>
                  <div class="btn-group">
                  </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                      <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">
                            <label>用户名</label>
                            <input type="text" class="input-xlarge" name="username">
                            <span id="tips" style="display: none;"></span>
                            <label>密码</label>
                            <input type="password" class="input-xxlarge" name="password">
                            <label>头像</label>
                            <input type="file"  name="avatar" id="avatar">
                            <label></label>
                            <img src="" alt="" id="preview" style="width: 100px;"> 
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交" />
                        </form>
                      </div>
                  </div>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                  </div>
                  <div class="modal-body">
                    
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                  </div>
                  <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                  </div>
                </div>
                
                <?php include_once('common/footer.php'); ?>
                    
            </div>
        </div>
    </div>
  </body>
</html>
<?php include_once('common/script.php');?>

<script>
  // 头像上传预览
  $("#avatar").change(function(){
    if (this.files && this.files[0]) {
      var reader = new FileReader();
      reader.onload = function (ev) {
        $('#preview').attr('src', ev.target.result);
      }
      reader.readAsDataURL(this.files[0]);
    }
  })

  $('input[name="username"]').change(function(){
    $.ajax({
      type: 'post',
      url: 'adminAdd.php?action=add',
      dataType: 'json',
      data: {
        username: $(this).val()
      },
      success: function(res) {
        if (res.code == 0) {
          $("#tips").css({
            "color": "red",
            "display": "inline-block"
          })
          $("#tips").html(res.msg);
          $('input[type="submit]').attr('disabled',true)
        }else {
          $("#tips").css({
            "color": "green",
            "display": "inline-block"
          })
          $("#tips").html(res.msg);
          $('input[type="submit]').attr('disabled',false)
        }
      }
    })
  })
</script>

